﻿package catchData;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;

/**
 * @author suwy
 * 2013-1-4
 * TODO catch data from 四川药械采购与监管
 */
public class Catch4fjc_scbid_gov_cn {
	private static Logger LOG = Logger.getLogger("Catch4fjc_scbid_gov_cn");
	private static HttpClient client;
	private static final String LOGIN_PAGE = "http://fjc.scbid.gov.cn/BidMedicineGJ.aspx";
	private static final String HOST = "fjc.scbid.gov.cn";
	private static long st;
	private static int pages = 201;
	private static Pattern viewStatePattern = Pattern
			.compile("<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"([^\"]+?)\" />");
	private static Pattern eventValidationPattern = Pattern
			.compile("<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"([^\"]+?)\" />");
	
	private void initHttpClient() {
		if (client == null) {
			MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
			client = new HttpClient(connectionManager);
			client.getParams().setCookiePolicy(
					CookiePolicy.BROWSER_COMPATIBILITY); // 配置Cookie策略为浏览器默认策略
			client.getParams().setParameter(
					"http.protocol.single-cookie-header", true);
			// client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
			// "UTF-8");
			client.getParams().setParameter(
					HttpMethodParams.HTTP_CONTENT_CHARSET, "gb2312");
			client.getHttpConnectionManager().getParams().setConnectionTimeout(
					300000);
			client.getHttpConnectionManager().getParams().setSoTimeout(300000);
		}
	}
	
	private void setHeaders(HttpMethod method) {
		method.getParams().setParameter("http.protocol.cookie-policy",
				CookiePolicy.BROWSER_COMPATIBILITY);
		method.setRequestHeader("accept",
						"image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
		method.setRequestHeader("referer", LOGIN_PAGE);
		method.setRequestHeader("accept-encoding",
				"UTF-8,GBK,GB2312,gzip,deflate");
		method.setRequestHeader("accept-language", "zh-CN");
		method.setRequestHeader("content-type",
				"application/x-www-form-urlencoded");
		method.setRequestHeader(
						"user-agent",
						"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9VbHRpbWF0ZS5leGUmbWFjPUY4RDgxQUY2MTgwQjQ3REQ5MUI5QkM4NjU3MzAwRkU3JnBhc3Nwb3J0PSZ2ZXJzaW9uPTIwMTAuNi4zLjYuMiZjcmFzaHR5cGU9MQ==&verify=b226e2d473bb8dbbb37568db14fee0e4; staticlogin:product=cboxf2010&act=login&info=ZmlsZW5hbWU9UG93ZXJXb3JkMjAxME94Zl9TcGVjaWFsLmV4ZSZtYWM9RjhEODFBRjYxODBCNDdERDkxQjlCQzg2NTczMDBGRTcmcGFzc3BvcnQ9JnZlcnNpb249MjAxMC42LjMuNS4yJmNyYXNodHlwZT0x&verify=94c65611a34cfb4964638346b072d76e; staticlogin:product=cboxf09&act=login&info=ZmlsZW5hbWU9UG93ZXJ3b3JkMjAwOU94Zi4yNTI2OS40MDExLmV4ZSZtYWM9RjhEODFBRjYxODBCNDdERDkxQjlCQzg2NTczMDBGRTcmcGFzc3BvcnQ9JnZlcnNpb249MjAwOS4wNS4yNS4zLjI3MiZjcmFzaHR5cGU9MQ==&verify=32e210faa8ce04f90dfc4225f98e80ac; aff-kingsoft-ciba; TheWorld)");
		method.setRequestHeader("host", HOST);
		method.setRequestHeader("connection", "Keep-Alive");
		method.setRequestHeader("cache-control", "no-cache");
	}
	
	private void saveDataFile(String sFolder, String data, long lid) {
		saveDataFile(sFolder, data, lid + "");
	}

	private void saveDataFile(String sFolder, String data, String sid) {
		File folder = new File(sFolder);
		if (!folder.exists()) folder.mkdirs();
		PrintWriter out = null;
		String saveFile = sFolder + "/" + sid + ".htm";
		try {
			out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(
					saveFile), "gb2312"));
			out.println(data);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
					out = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		LOG.info("It got the file " + saveFile.replaceAll("\\\\", "/"));
	}
	
	private String getViewState(String content) {
		String viewState = "";
		Matcher matcher = null;
		matcher = viewStatePattern.matcher(content);
		if (matcher.find()) {
			viewState = matcher.group(1);
		}
		return viewState;
	}

	private String getEventValidation(String content) {
		String eventValidAction = "";
		Matcher matcher = null;
		matcher = eventValidationPattern.matcher(content);
		if (matcher.find()) {
			eventValidAction = matcher.group(1);
		}
		return eventValidAction;
	}
	
	public static void main(String[] args) {
		st = System.currentTimeMillis();
		String viewState1 = "";
		String eventValidation1 ="";
		String path = "E:/downloadData/国家基本药物挂网药品目录";
		String str = "";
		String n = "";
		int k = 0;
		PostMethod post = null;
		try {
			Catch4fjc_scbid_gov_cn instance = new Catch4fjc_scbid_gov_cn();
			instance.initHttpClient();
			PostMethod first = new PostMethod(LOGIN_PAGE);
			instance.setHeaders(first);
			client.executeMethod(first);
			str = first.getResponseBodyAsString();
			first.releaseConnection();
			for (int i = 1; i <= pages; i++) {
				viewState1 = instance.getViewState(str);
				eventValidation1 = instance.getEventValidation(str);
				if (k == 21 && i <40) {
					k = 2;
				}else if (k == 22 && i > 40) {
					k = 2;
				}
				n = String.format("%02d", k);
				NameValuePair eventTarget = new NameValuePair("__EVENTTARGET","GridMedicine$ctl01$ctl"+n);
				NameValuePair eventValidation = new NameValuePair("__EVENTVALIDATION", eventValidation1);
				NameValuePair viewState = new NameValuePair("__VIEWSTATE", viewState1);
//				System.out.println("代号："+n+"页数："+i);
				post = new PostMethod(LOGIN_PAGE);
				instance.setHeaders(post);
				post.setRequestBody(new NameValuePair[] {eventTarget,eventValidation,viewState});
				client.executeMethod(post);
				str = post.getResponseBodyAsString();
//				System.out.println(str);
				instance.saveDataFile(path, str, i);
				++k;
				post.releaseConnection();
			}
			LOG.info("It's over ! It spended "+(System.currentTimeMillis() - st)/1000 + " seconds.");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}